import { ElNotification } from 'element-plus'
import { useSiteConfig } from '../store/siteConfig'
import { useAdminInfo } from '../store/adminInfo'
import router from '../router'
/**
 * 缓存清理接口
 */
export function postClearCache(type) {
    // return createAxios(
    //     {
    //         url: clearCacheUrl,
    //         method: 'POST',
    //         data: {
    //             type: type,
    //         },
    //     },
    //     {
    //         showSuccessMessage: true,
    //     }
    // )
    return {}
}

export function logout() {
    const adminInfo = useAdminInfo()
    adminInfo.removeToken()
    router.push('login')
    // return createAxios({
    //     url: url + 'logout',
    //     method: 'POST',
    //     data: {
    //         refreshToken: adminInfo.getToken('refresh'),
    //     },
    // })
    return {}
}

/**
 * 上传文件
 */
export function fileUpload(fd, params = {}, forceLocal = false, config = {}){
    let errorMsg = ''
    const file = fd.get('file')
    const siteConfig = useSiteConfig()

    if (!file.name || typeof file.size == 'undefined') {
        errorMsg = i18n.global.t('utils.The data of the uploaded file is incomplete!')
    } else if (!checkFileMimetype(file.name, file.type)) {
        errorMsg = i18n.global.t('utils.The type of uploaded file is not allowed!')
    } else if (file.size > siteConfig.upload.maxsize) {
        errorMsg = i18n.global.t('utils.The size of the uploaded file exceeds the allowed range!')
    }
    if (errorMsg) {
        return new Promise((resolve, reject) => {
            ElNotification({
                type: 'error',
                message: errorMsg,
            })
            reject(errorMsg)
        })
    }

    if (!forceLocal && uploadExpandState() == 'enable') {
        return uploadExpand(fd, params, config)
    }

    return {}
}

/**
 * 远程下拉框数据获取
 */
export function getSelectData(remoteUrl, q, params) {
    return {}
}

/**
 * 生成一个控制器的：增、删、改、查、排序的操作
 */
export class baTableApi {
    controllerUrl
    actionUrl

    constructor(controllerUrl) {
        this.controllerUrl = controllerUrl
        this.actionUrl = new Map([
            ['index', controllerUrl + 'index'],
            ['add', controllerUrl + 'add'],
            ['edit', controllerUrl + 'edit'],
            ['del', controllerUrl + 'del'],
            ['sortable', controllerUrl + 'sortable'],
        ])
    }

    index(filter = {}) {
        return false
    }

    edit(params) {
        return {}
    }

    del(ids) {
        return {}
    }

    postData(action, data) {
        return {}
    }

    sortableApi(id, targetId) {
        return {}
    }
}